﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class AddUpdateImages : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            load_RecordStatus();

            int iAl = int.Parse(Request.QueryString["al"].ToString());
            aIgnore.HRef = "ImagesManager.aspx?al=" + iAl.ToString();

            if (Request.QueryString["id"] != null)
            {
                int iID = int.Parse(Request.QueryString["id"].ToString());
                load_Images(iID);
            }
        }
    }

    public void load_Images(int iID)
    {
        DataSet dsImages = BusLogic.Images.get_IMAGES(iID);

        txtName.Text = dsImages.Tables[0].Rows[0]["NAME"].ToString();

        if (!String.IsNullOrEmpty(dsImages.Tables[0].Rows[0]["IMAGE"].ToString()))
        {
            imgImages.Src = dsImages.Tables[0].Rows[0]["IMAGE"].ToString();
            hfImage.Value = dsImages.Tables[0].Rows[0]["IMAGE"].ToString();
        }
        else
        {
            imgImages.Src = "~/App_Themes/Images/toolbar/Images.png";
        }

        txtSorted.Text = dsImages.Tables[0].Rows[0]["SORTED"].ToString();
        rdlRecordStatus.SelectedValue = dsImages.Tables[0].Rows[0]["RECORD_STATUS"].ToString();
    }

    public void load_RecordStatus()
    {
        ListItem item = new ListItem();

        item.Value = "True";
        item.Text = "Công bố";
        item.Selected = true;
        rdlRecordStatus.Items.Add(item);

        item = new ListItem();
        item.Value = "False";
        item.Text = "Chờ";
        rdlRecordStatus.Items.Add(item);
    }



    protected void lbtSave_Click(object sender, EventArgs e)
    {
        int iAl = int.Parse(Request.QueryString["al"].ToString());

        if (Request.QueryString["id"] == null)
        {
            string strImage = hfImage.Value.ToString();
            BusLogic.Images.add_IMAGES(txtName.Text, iAl, strImage, int.Parse(txtSorted.Text), Boolean.Parse(rdlRecordStatus.SelectedValue));            
        }
        else
        {
            int iID = int.Parse(Request.QueryString["id"].ToString());
            string strImage = hfImage.Value.ToString();            
            BusLogic.Images.update_IMAGES(iID, txtName.Text, iAl, strImage, int.Parse(txtSorted.Text), Boolean.Parse(rdlRecordStatus.SelectedValue));            
        }

        Response.Redirect("ImagesManager.aspx?al=" + iAl.ToString());
    }
}
